"use strict";
const superagent = require('superagent');
const moment = require('moment');

let TEST_DOMAIN = 'http://localhost:3014';

const productsVideoUniTest = (function () {

    function productsVideoUniTest() {

    }

    /*
    * 设置当前测试用例执行的环境
    * */
    productsVideoUniTest.setTestDomain = function (domain) {
        TEST_DOMAIN = domain;
    };

    /**
     * 创建作品
     *
     * @author mk
     * @date 2018-06-04
     *
     * @param {Object} param      测试参数
     * @param {String} param.token       令牌
     * @param {String} param.design_id   模板id
     * @param {String} param.content    视频内容
     * @param {Function} callback
     */
    productsVideoUniTest.crateVideoDoc = function (param,callback) {
        let url = TEST_DOMAIN + '/v1/products/videos/crateVideoDoc';
        let para = {
            token : param.token,
            design_id : param.design_id,
            content : param.content
        };
        superagent.post(url)
            .send(para)
            .end(function (error, result) {
                if (error || !result) {
                    if(callback) callback(error || '请求失败');
                    return;
                }
                let response = JSON.parse(result.text);
                if (!response || response.code !== 10000) {
                    if (callback) callback(response.msg || '请求失败');
                    return;
                }
                if (callback) callback(null, response.data);
            });
    };

    /**
     * 更新作品
     *
     * @author mk
     * @date 2018-06-05
     *
     * @param {Object} param        测试参数
     * @param {String} param.token         令牌
     * @param {String} param.product_id    作品的唯一标识
     * @param {String} param.content       视频内容
     * @param {Function} callback
     */
    productsVideoUniTest.updateVideoDoc = function (param, callback) {
        let url = TEST_DOMAIN + '/v1/products/videos/updateVideoDoc';
        let para = {
            token : param.token,
            product_id : param.product_id,
            content : param.content
        };
        superagent.post(url)
            .send(para)
            .end(function (error, result) {
                if (error || !result) {
                    if(callback) callback(error || '请求失败');
                    return;
                }
                let response = JSON.parse(result.text);
                if (!response || response.code !== 10000) {
                    if (callback) callback(response.msg || '请求失败');
                    return;
                }
                if (callback) callback(null, response.msg);
            });
    };


    /**
     * 获取作品数据
     *
     * @author mk
     * @date 2018-06-05
     *
     * @param {Object} param        测试参数
     * @param {String} param.token         令牌
     * @param {String} param.product_id    作品的唯一标识
     * @param {Function} callback
     */
    productsVideoUniTest.getVideoDocContent = function (param, callback) {
        let url = TEST_DOMAIN + '/v1/products/videos/getVideoDocContent';
        let para = {
            token : param.token,
            product_id : param.product_id
        };
        superagent.post(url)
            .send(para)
            .end(function (error, result) {
                if (error || !result) {
                    if(callback) callback(error || '请求失败');
                    return;
                }
                let response = JSON.parse(result.text);
                if (!response || response.code !== 10000) {
                    if (callback) callback(response.msg || '请求失败');
                    return;
                }
                if (callback) callback(null, response.data);
            });
    };


    /**
     * 获取作品预览
     *
     * @author mk
     * @date 2018-06-05
     *
     * @param {Object} param        测试参数
     * @param {String} param.token         令牌
     * @param {String} param.product_id    作品的唯一标识
     * @param {Function} callback
     */
    productsVideoUniTest.typesetVideoDoc = function (param, callback) {
        let url = TEST_DOMAIN + '/v1/products/videos/typesetVideoDoc';

        let para = {
            token : param.token,
            product_id : param.product_id
        };
        // 轮询接口
        function request() {
            console.log('productsVideoUniTest.typesetVideoDoc request: ' + new moment().format('YYYY-MM-DD HH:mm:ss'));
            superagent.get(url)
                .send(para)
                .end(function (error, result) {
                    result = result ? result : {};
                    let response = error ? {} : JSON.parse(result.text || '{}');
                    if (error || response.code !== 10000) {
                        console.log('request Err: ' + error + ' msg: ' + response.msg);
                        if(callback) return callback(error, null);
                        return;
                    }
                    let data = response.data;
                    // 生成失败
                    if (data.status === 2) {
                        console.log("生成失败: " + JSON.stringify(data));
                        if(callback) return callback(error, data);
                        return;
                    }
                    // 生成成功
                    if (data.status === 3) {
                        console.log("生成成功: " + JSON.stringify(data));
                        if(callback) return callback(error, data);
                        return;
                    }
                    // 生成中
                    let progressInfo = data.progress_info;
                    let nextInterval = data.progress_info.next_interval;

                    console.log("productsVideoUniTest.typesetVideoDoc: 生成中: " + JSON.stringify(progressInfo));
                    console.log('productsVideoUniTest.typesetVideoDoc: ' + nextInterval + 's 后重新查询');
                    setTimeout(function () {
                        request();
                    }, nextInterval * 1000);

                });
        }
        request();
    };


    /**
     *productsVideoUniTest  CrateVideoDoc参数正确
     */
    productsVideoUniTest.productCrateVideoDocTestCase1 = function (callback) {
        let param = {
            token : "101002080503010120AVWB1HT3DI9RP3LJ24T38FXFOW1P6LGACWRLFB06020C5FB6CCCB060C0D8E5DDC9EA43IWJ6X1QUBJOU7IJ9XQALG932W5III7HFMSAXACHXX",
            design_id : 9103,
            content :"[{\"id\":1333693045,\"type\":1,\"max\":14,\"text\":\"新微1零售\"},{\"id\":1333098879,\"type\":2,\"image\":\"http://itbour-user.oss-cn-hangzhou.aliyuncs.com/custAni/assets/images/U670651/2017/12/15/191805684_AvsVWXuWUU/img_6.png\",\"real_w\":1920,\"real_h\":1080,\"w\":1920,\"h\":1080},{\"id\":1332788565,\"type\":1,\"max\":14,\"text\":\"全场商品5折起\"},{\"id\":1333099903,\"type\":2,\"image\":\"http://itbour-user.oss-cn-hangzhou.aliyuncs.com/custAni/assets/images/U670651/2017/12/15/191805684_YpIGJ6nOCU/img_7.png\",\"real_w\":640,\"real_h\":595,\"w\":640,\"h\":595},{\"id\":1333100810,\"type\":2,\"image\":\"http://itbour-user.oss-cn-hangzhou.aliyuncs.com/custAni/assets/images/U670651/2017/12/15/191805684_83R2pFHSSN/img_8.png\",\"real_w\":640,\"real_h\":595,\"w\":640,\"h\":595},{\"id\":1333101463,\"type\":2,\"image\":\"http://itbour-user.oss-cn-hangzhou.aliyuncs.com/custAni/assets/images/U670651/2017/12/15/191805684_bKmIIXidBy/img_9.png\",\"real_w\":640,\"real_h\":595,\"w\":640,\"h\":595},{\"id\":1332798047,\"type\":1,\"max\":4,\"text\":\"回馈\"},{\"id\":1332797116,\"type\":1,\"max\":8,\"text\":\"新老用户\"},{\"id\":1332795160,\"type\":1,\"max\":8,\"text\":\"新品促销\"},{\"id\":1332796306,\"type\":1,\"max\":10,\"text\":\"新品超值购\"},{\"id\":1333102882,\"type\":2,\"image\":\"http://itbour-user.oss-cn-hangzhou.aliyuncs.com/custAni/assets/images/U670651/2017/12/15/191805682_sDiKjR2Wev/img_1.png\",\"real_w\":1408,\"real_h\":927.3,\"w\":1408,\"h\":927.3},{\"id\":1332801234,\"type\":1,\"max\":18,\"text\":\"春季热卖 998元\"},{\"id\":1332799127,\"type\":1,\"max\":8,\"text\":\"特惠促销\"},{\"id\":1333103441,\"type\":2,\"image\":\"http://itbour-user.oss-cn-hangzhou.aliyuncs.com/custAni/assets/images/U670651/2017/12/15/191805684_6x2njK4Md7/img_5.png\",\"real_w\":768,\"real_h\":714,\"w\":768,\"h\":714},{\"id\":1332790025,\"type\":1,\"max\":14,\"text\":\"售价：998元\"},{\"id\":1333104026,\"type\":2,\"image\":\"http://itbour-user.oss-cn-hangzhou.aliyuncs.com/custAni/assets/images/U670651/2017/12/15/191805683_zpUd6GWx5d/img_4.png\",\"real_w\":768,\"real_h\":714,\"w\":768,\"h\":714},{\"id\":1332790745,\"type\":1,\"max\":14,\"text\":\"售价：998元\"},{\"id\":1333104665,\"type\":2,\"image\":\"http://itbour-user.oss-cn-hangzhou.aliyuncs.com/custAni/assets/images/U670651/2017/12/15/191805683_bO8Z6UFy5e/img_3.png\",\"real_w\":768,\"real_h\":714,\"w\":768,\"h\":714},{\"id\":1332791248,\"type\":1,\"max\":14,\"text\":\"售价：998元\"},{\"id\":1332799947,\"type\":1,\"max\":8,\"text\":\"春季新品\"},{\"id\":1332793992,\"type\":1,\"max\":32,\"text\":\"抢购热线：027-8888888\"}]"
        };
        productsVideoUniTest.crateVideoDoc(param,function (err,data) {
            return callback(err, JSON.stringify(data));
        });
    };

    productsVideoUniTest.productUpdateVideoDocDocTestCase1 = function (callback) {
        let param = {
            token : "101002080503010120AVWB1HT3DI9RP3LJ24T38FXFOW1P6LGACWRLFB06020C5FB6CCCB060C0D8E5DDC9EA43IWJ6X1QUBJOU7IJ9XQALG932W5III7HFMSAXACHXX",
            product_id : 2358,
            content :"[{\"id\":1333693045,\"type\":1,\"max\":14,\"text\":\"新微1234零售\"},{\"id\":1333098879,\"type\":2,\"image\":\"http://itbour-user.oss-cn-hangzhou.aliyuncs.com/custAni/assets/images/U670651/2017/12/15/191805684_AvsVWXuWUU/img_6.png\",\"real_w\":1920,\"real_h\":1080,\"w\":1920,\"h\":1080},{\"id\":1332788565,\"type\":1,\"max\":14,\"text\":\"全场商品5折起\"},{\"id\":1333099903,\"type\":2,\"image\":\"http://itbour-user.oss-cn-hangzhou.aliyuncs.com/custAni/assets/images/U670651/2017/12/15/191805684_YpIGJ6nOCU/img_7.png\",\"real_w\":640,\"real_h\":595,\"w\":640,\"h\":595},{\"id\":1333100810,\"type\":2,\"image\":\"http://itbour-user.oss-cn-hangzhou.aliyuncs.com/custAni/assets/images/U670651/2017/12/15/191805684_83R2pFHSSN/img_8.png\",\"real_w\":640,\"real_h\":595,\"w\":640,\"h\":595},{\"id\":1333101463,\"type\":2,\"image\":\"http://itbour-user.oss-cn-hangzhou.aliyuncs.com/custAni/assets/images/U670651/2017/12/15/191805684_bKmIIXidBy/img_9.png\",\"real_w\":640,\"real_h\":595,\"w\":640,\"h\":595},{\"id\":1332798047,\"type\":1,\"max\":4,\"text\":\"回馈\"},{\"id\":1332797116,\"type\":1,\"max\":8,\"text\":\"新老用户\"},{\"id\":1332795160,\"type\":1,\"max\":8,\"text\":\"新品促销\"},{\"id\":1332796306,\"type\":1,\"max\":10,\"text\":\"新品超值购\"},{\"id\":1333102882,\"type\":2,\"image\":\"http://itbour-user.oss-cn-hangzhou.aliyuncs.com/custAni/assets/images/U670651/2017/12/15/191805682_sDiKjR2Wev/img_1.png\",\"real_w\":1408,\"real_h\":927.3,\"w\":1408,\"h\":927.3},{\"id\":1332801234,\"type\":1,\"max\":18,\"text\":\"春季热卖 998元\"},{\"id\":1332799127,\"type\":1,\"max\":8,\"text\":\"特惠促销\"},{\"id\":1333103441,\"type\":2,\"image\":\"http://itbour-user.oss-cn-hangzhou.aliyuncs.com/custAni/assets/images/U670651/2017/12/15/191805684_6x2njK4Md7/img_5.png\",\"real_w\":768,\"real_h\":714,\"w\":768,\"h\":714},{\"id\":1332790025,\"type\":1,\"max\":14,\"text\":\"售价：998元\"},{\"id\":1333104026,\"type\":2,\"image\":\"http://itbour-user.oss-cn-hangzhou.aliyuncs.com/custAni/assets/images/U670651/2017/12/15/191805683_zpUd6GWx5d/img_4.png\",\"real_w\":768,\"real_h\":714,\"w\":768,\"h\":714},{\"id\":1332790745,\"type\":1,\"max\":14,\"text\":\"售价：998元\"},{\"id\":1333104665,\"type\":2,\"image\":\"http://itbour-user.oss-cn-hangzhou.aliyuncs.com/custAni/assets/images/U670651/2017/12/15/191805683_bO8Z6UFy5e/img_3.png\",\"real_w\":768,\"real_h\":714,\"w\":768,\"h\":714},{\"id\":1332791248,\"type\":1,\"max\":14,\"text\":\"售价：998元\"},{\"id\":1332799947,\"type\":1,\"max\":8,\"text\":\"春季新品\"},{\"id\":1332793992,\"type\":1,\"max\":32,\"text\":\"抢购热线：027-8888888\"}]"
        };
        productsVideoUniTest.updateVideoDoc(param,function (err,data) {
            return callback(err, JSON.stringify(data));
        });
    };


    productsVideoUniTest.productGetVideoDocContentTestCase1 = function (callback) {
        let param = {
            token : "101002080503010120AVWB1HT3DI9RP3LJ24T38FXFOW1P6LGACWRLFB06020C5FB6CCCB060C0D8E5DDC9EA43IWJ6X1QUBJOU7IJ9XQALG932W5III7HFMSAXACHXX",
            product_id : 2358
        };
        productsVideoUniTest.getVideoDocContent(param,function (err,data) {
            return callback(err, JSON.stringify(data));
        });
    };


    productsVideoUniTest.productTypesetVideoTestCase1 = function (callback) {
        let param = {
            token : "101002080503010120AVWB1HT3DI9RP3LJ24T38FXFOW1P6LGACWRLFB06020C5FB6CCCB060C0D8E5DDC9EA43IWJ6X1QUBJOU7IJ9XQALG932W5III7HFMSAXACHXX",
            product_id : 2358
        };
        productsVideoUniTest.typesetVideoDoc(param,function (err,data) {
            return callback(err, JSON.stringify(data));
        });
    };




    return productsVideoUniTest;
})();
module.exports = productsVideoUniTest;



